package org.cinvestav.computacion.matias.onlyangel.agentes;
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;

public class Coach extends Agent{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	protected void setup(){
		addBehaviour(new TickerBehaviour(this,5000){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			protected void onTick(){
				int[] p;

				try{
					DocumentBuilderFactory docBuilderFactory=DocumentBuilderFactory.newInstance();
					DocumentBuilder docBuilder=docBuilderFactory.newDocumentBuilder();
					Document doc=docBuilder.parse(new File("jugada.xml"));

					doc.getDocumentElement().normalize();
					NodeList listofgroups=doc.getElementsByTagName("vector");
					int totalgroups=listofgroups.getLength();
					p=new int[totalgroups];

					System.out.println();
					for(int s=0; s<listofgroups.getLength(); s++){
						Node firstpersonnode=listofgroups.item(s);
						if(firstpersonnode.getNodeType()==Node.ELEMENT_NODE){
							Element firstpersonelement=(Element)firstpersonnode;
							NodeList firstnamelist=firstpersonelement.getElementsByTagName("elemento");
							Element firstnameelement=(Element)firstnamelist.item(0);
							NodeList textfnlist=firstnameelement.getChildNodes();
							p[s]=(int)(100*Double.parseDouble(new String(((Node)textfnlist.item(0)).getNodeValue().trim())));
							System.out.println(p[s]);
						}
					}
				}catch(SAXParseException err){
					System.out.println("** Parsing error, line "+err.getLineNumber()+", uri "+err.getSystemId());
					System.out.println(" "+err.getMessage());
				}catch(SAXException ie){
					Exception x=ie.getException();
					((x==null) ? ie : x).printStackTrace();
				}catch(Throwable t){
					t.printStackTrace();
				}
			}
		});
	}
}
